package com.example.mobile2021606185zhn0618;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.w3c.dom.Text;

import java.util.List;

public class UsersRViewAdapter extends RecyclerView.Adapter<UsersRViewAdapter.ViewHolder> {

    private List<Users> list;

    public UsersRViewAdapter(List<Users> list) {
        this.list = list;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvItemId, tvItemName, tvItemAge, tvItemScore;
        Button btnItemUpdate, btnItemDelete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvItemId = itemView.findViewById(R.id.tvItemId);
            tvItemName = itemView.findViewById(R.id.tvItemName);
            tvItemAge = itemView.findViewById(R.id.tvItemAge);
            tvItemScore = itemView.findViewById(R.id.tvItemScore);
            btnItemDelete = itemView.findViewById(R.id.btnItemDelete);
            btnItemUpdate = itemView.findViewById(R.id.btnItemUpdate);

        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_rv_main_user, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull UsersRViewAdapter.ViewHolder holder, int position) {
        Users users = list.get(position);
        holder.tvItemId.setText(String.valueOf(users.get_id()));
        holder.tvItemName.setText(users.getName());
        holder.tvItemAge.setText(String.valueOf(users.getAge()));
        holder.tvItemScore.setText(String.valueOf(users.getScore()));
        holder.btnItemDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view = LayoutInflater.from(v.getContext()).inflate(R.layout.dialog_insert,null);

                new MyDataAdapter().delete(v.getContext(),list.get(position).get_id());
                list.clear();
                list.addAll(new MyDataAdapter().QueryAll(v.getContext()));
                notifyDataSetChanged();
            }
        });
        holder.btnItemUpdate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final View view = LayoutInflater.from(v.getContext()).inflate(R.layout.dialog_insert,null);
                final EditText edtTxtDialogName = view.findViewById(R.id.edtTxtDialogName);
                final EditText edtTxtDialogAge = view.findViewById(R.id.edtTxtDialogAge);
                final EditText edtTxtDialogScore = view.findViewById(R.id.edtTxtDialogScore);
                edtTxtDialogName.setText(list.get(position).getName());
                edtTxtDialogAge.setText(String.valueOf(list.get(position).getAge()));
                edtTxtDialogScore.setText(String.valueOf(list.get(position).getScore()));
                  new AlertDialog.Builder(v.getContext())
                          .setTitle("更新数据")
                          .setView(view)
                          .setPositiveButton("更新", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  String name = edtTxtDialogName.getText().toString().trim();
                                  int age = Integer.valueOf(edtTxtDialogAge.getText().toString().trim());
                                  float score = Integer.valueOf(edtTxtDialogScore.getText().toString().trim());


                                  new MyDataAdapter().update(v.getContext(),
                                          list.get(position).get_id(),
                                          new Users(name,age,score));
                                  list.clear();
                                  list.addAll(new MyDataAdapter().QueryAll(v.getContext()));
                                  notifyDataSetChanged();
                              }
                          })
                          .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  final EditText edtTxDialogName = view.findViewById(R.id.edtTxtDialogName);
                                  final EditText edtTxDialogAge = view.findViewById(R.id.edtTxtDialogAge);
                                  final EditText edtTxDialogScore = view.findViewById(R.id.edtTxtDialogScore);
                               edtTxDialogName.setText(list.get(position).getName());
                                  edtTxDialogAge.setText(String.valueOf(list.get(position).getAge()));
                                  edtTxDialogScore.setText(String.valueOf(list.get(position).getName()));
                              }

                          })
                          .create()
                          .show();
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


}

